package com.woniuxy.service.impl;

import com.woniuxy.entity.Menu;
import com.woniuxy.mapper.MenuMapper;
import com.woniuxy.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * FileName: MenuServiceImpl
 * Author: wujiajun
 * Date:   2025/1/13 16:04
 * Description:DOTO
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;

    /**
     *数据转化为一级菜单中包含二级菜单结构
     *1:先找出所有夫菜单
     * 2：循环夫菜单，去所有菜单的集合中找对应的子菜单
     */
    @Override
    public List<Menu> queryUserMenu(Integer UserId) {
        List<Menu> menus = menuMapper.queryUserMenu(UserId);
        List<Menu> parentMenu = menus.stream().filter(e -> e.getParentId().equals(-1)).collect(Collectors.toList());
        for (Menu pmenu : parentMenu) {
            List<Menu> ChildrenMenu = new ArrayList<>();
            for (Menu menu : menus){
                if (menu.getParentId().equals(pmenu.getId())){
                    ChildrenMenu.add(menu);
                }
            }
            pmenu.setChildrenMenu(ChildrenMenu);
        }
        return parentMenu;
    }
}
